<?php
namespace app\common\service;


use think\facade\Db;

class TransferdataNew extends Base {
    static $AUTH_CHECK = false; //默认必须登录
    public $key = '9CEjWlh8My';
    public $method_arr = [
        'neworder' ,
        'refund',
        'send_order'
    ];
    public function index()
    {
        file_put_contents(RUNTIME_PATH.'/duijie.txt',date("Y-m-d H:i:s").'::'.json_encode(input())."\r\n",FILE_APPEND);
//        $rules = [
//            'order_sn'  => '123',
//            'username'   => '18866677777',
//            'truename'   => '1',
//            'mobile'   => '18866677777',
//            'total_shop_price' => '100',
//            'total_pv' => '50',
//            'province' => '山东',
//            'city' => '临沂',
//            'district' => '兰山',
//            'address' => 'ifc',
//            'product_name' => '测试',
//        ];
//        dump(json_encode($rules));exit;
        Db::startTrans();
        $params = input('post.');
        $data = [];
        $data['unique_no'] = $params['order_sn']?:'';
        $data['order_sn'] = $params['order_sn']?:'';
        $data['type'] = $params['push_type']?:'';
        $data['post_data'] = json_encode($params)?:'';
        $data['status'] = 0;
        $data['w_time'] = time();
        $check_res = $this->checkdata($params);
        if(!$check_res['status']){
            $data['err_status'] = 1;
            $data['err_msg'] = $check_res['msg'];
        }
        $data['err_msg'] = $check_res['msg']??'';
        Db::name('transfer_data')->insert($data);
        if(!$check_res['status']){
            return $check_res;
        }
        //$params_data = json_decode(htmlspecialchars_decode($params['post_data']),true);
        if($params['push_type']=='neworder'){
            $res = logic('TransferData')->add_order($params,$params['unique_no']);
        }elseif($params['push_type']=='refund'){
            $res = logic('TransferData')->cancel_order($params);
        }elseif($params['push_type']=='send_order'){
            $res = logic('TransferData')->send_order($params);
        }
        if(!$res['status']){
            return $res;
        }
        Db::commit();
        echo 'success';exit;
        return $res;

    }

    public function checkdata($params){

        if(empty($params['sign'])){
            $result = [
                'status' => false,
                'msg' => '签名不可为空',
                'data' => ''
            ];

            return $result;
        }
        $sign = $params['sign'];
        $test = $params['test']??0;

        $order_no = $params['order_sn']??'';
        if(empty($order_no)){
            $result = [
                'status' => false,
                'msg' => '订单号不可为空',
                'data' => ''
            ];
            return $result;
        }
        $key = $this->key;
        //签名字符串
        unset($params['sign']);
        unset($params['test']);
        unset($params['service']);
        unset($params['method']);
        //unset($params['user_id']);
        ksort($params);
        $sign_str = '';
        foreach($params as $k=>$v){
            if(!$v){
                continue;
            }
            if($k=='product_list'){

                $v = htmlspecialchars_decode(htmlspecialchars_decode($v));
            }
            $sign_str .= $k.'='.urlencode($v).'&';
        }
        $sign_str = rtrim($sign_str,'&');
        $sign_str = $sign_str.$key;

        if(strtolower(md5($sign_str))!=$sign && !$test){
            $result = [
                'status' => false,
                'msg' => '签名错误',
                'data' => ''
            ];
            return $result;
        }
        $method_arr = $this->method_arr;
        if(!in_array($params['push_type'],$method_arr)){
            $result = [
                'status' => false,
                'msg' => '方法不存在',
                'data' => ''
            ];
            return $result;
        }
        $data_exit = Db::name('transfer_data')->where('unique_no',$params['unique_no'])->find();
        if($data_exit){
            $result = [
                'status' => false,
                'msg' => '订单已存在',
                'data' => ''
            ];
            return $result;
        }
        $result = [
            'status' => true,
            'msg' => '',
            'data' => ''
        ];
        return $result;
    }

}
